package com.zy.sort;

import java.util.List;

/**
 * @description: 冒泡排序 时间复杂度 O(n^2) 空间复杂的O(1)
 * @author: zhaoyi
 * @create: 2019-10-31 21:02
 **/

public class Bubbling<T extends Comparable<? super T>> {

    /**
     * 冒泡排序
     *
     * @param list
     */
    public void sort(List<T> list) {
        if (list == null || list.size() <= 1) {
            return;
        }
        int endIndex = list.size() - 1;
        boolean flag = true;
        while (endIndex >= 0 && flag) {
            flag = false;
            for (int i = 0; i < endIndex; i++) {
                T t1 = list.get(i);
                T t2 = list.get(i + 1);
                if (t1.compareTo(t2) > 0) {
                    flag = true;
                    T tmp = t1;
                    list.set(i, t2);
                    list.set(i + 1, tmp);
                }
            }
            --endIndex;
        }
    }


}
